package guoanmaker.provider.business.service;


import java.util.List;

import guoanmaker.provider.business.model.PersonalPlatform;
import guoanmaker.provider.business.repository.PersonalPlatformSeriveRepository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;



@Service
public class PersonalPlatformSerive implements PersonalPlatformSeriveinterface {

	
	@Autowired
	private PersonalPlatformSeriveRepository PersonalPlatformSerive;
	
	@PersistenceContext
	private EntityManager manager;
	
	@Override
	@Transactional
	public void insertPersonalPlatform(Workbook wb) {
				Sheet sheet = wb.getSheetAt(0);
				int totcount = sheet.getLastRowNum();
				for(int i=0;i<totcount;i++){
					Row row = sheet.getRow(i + 1);
					if(i==0){
					}
					if(row.getCell(0)!=null){
				          row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
				     }
					if(row.getCell(1)!=null){
						row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
					}
					if(row.getCell(2)!=null){
						row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
					}
					if(row.getCell(3)!=null){
						row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
					}
					if(row.getCell(4)!=null){
						row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
					}
					if(row.getCell(5)!=null){
						row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
					}
					if(row.getCell(6)!=null){
						row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
					}
					if(row.getCell(7)!=null){
						row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
					}
					if(row.getCell(8)!=null){
						row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
					}
					if(row.getCell(9)!=null){
						row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
					}
					
					PersonalPlatform  personalPlatform=new PersonalPlatform();
					
					if(row.getCell(0)!=null){
						personalPlatform.setName(row.getCell(0).getStringCellValue());
					}
					if(row.getCell(1)!=null){
						personalPlatform.setCompanyProject(row.getCell(1).getStringCellValue());
					}
					if(row.getCell(2)!=null){
						
						personalPlatform.setPersonnelPost(row.getCell(2).getStringCellValue());
					}
					if(row.getCell(3)!=null){
						personalPlatform.setContactNumber(row.getCell(3).getStringCellValue());
					}
					if(row.getCell(4)!=null){
						personalPlatform.setIntroduction(row.getCell(4).getStringCellValue());
					}
					if(row.getCell(5)!=null){
						personalPlatform.setRound(row.getCell(5).getStringCellValue());
					}
					
					if(row.getCell(6)!=null){
						personalPlatform.setFinancingDemand(row.getCell(6).getStringCellValue());
					}		
					if(row.getCell(7)!=null){
						personalPlatform.setIntroduce(row.getCell(7).getStringCellValue());
					}		
					if(row.getCell(8)!=null){
						personalPlatform.setEntrepreneurialTeam(row.getCell(8).getStringCellValue());
					}		
					if(row.getCell(9)!=null){
						personalPlatform.setTeamPresentation(row.getCell(9).getStringCellValue());
					}		
					PersonalPlatformSerive.save(personalPlatform);
					
					
				}
		
	}

	@Override
	public Page<PersonalPlatform> selectAllPersonalPlatform(Integer page,
			Integer size) {
		Sort sort = new Sort(Sort.Direction.DESC, "createtime");
		Pageable pageable  = new PageRequest(page,size,sort);
		Page<PersonalPlatform> personalPlatform = PersonalPlatformSerive.findAll(pageable);
		
		return personalPlatform;
	}

	@Override
	public PersonalPlatform selectOnePersonalPlatform(String id) {
		
		return PersonalPlatformSerive.findOne(id);
	}

	@Override
	public List<PersonalPlatform> selectPersonalPlatform() {
		return PersonalPlatformSerive.findAll();
	}

	@Override
	public PersonalPlatform addorUpdatePersonalPlatform (PersonalPlatform personalPlatform) {
		
		return PersonalPlatformSerive.save(personalPlatform);
	}

	@Override
	@Transactional
	public void deletePersonalPlatform(String id) {
		
		String jpql = "delete from PersonalPlatform p  where p.id=:id"; 
		Query query = manager.createQuery(jpql).setParameter("id", id);
		int reuslt=query.executeUpdate();
		System.out.println(reuslt);
		
	}


	
	
	

}
